package juc.辅助类;

import java.util.concurrent.CountDownLatch;

/**
 * 倒计时器
 *
 * @author: 张从龙
 * time: 2022/11/13 13:39
 */
public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        //创建倒计时器
        CountDownLatch countDownLatch = new CountDownLatch(6);

        //6个同学陆续离开教室
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "号离开");
                //计数减1
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }

        //阻塞，等所有走了
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName() + "班长离开");
    }
}